package com.ucpaas.sms.action.sysconf;

import com.jsmsframework.common.dto.JsmsPage;
import com.jsmsframework.common.entity.JsmsParam;
import com.jsmsframework.common.enums.schedule.ParamStatus;
import com.jsmsframework.common.enums.schedule.ParamType;
import com.jsmsframework.common.service.JsmsParamService;
import com.jsmsframework.common.util.JsonUtil;
import com.ucpaas.sms.action.BaseAction;
import com.ucpaas.sms.service.sysconf.ParamConfService;
import com.ucpaas.sms.util.web.StrutsUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
/**
 * 系统设置-参数设置
 * 
 * @author zenglb
 */
@SuppressWarnings("Duplicates")
@Controller
@Scope("prototype")
@Results({ @Result(name = "query", location = "/WEB-INF/content/sysconf/smspParamConf/query.jsp"),
		@Result(name = "edit", location = "/WEB-INF/content/sysconf/smspParamConf/edit.jsp") })
public class SmspParamConfAction extends BaseAction {
	private static final long serialVersionUID = -1234214414349439397L;

	private static Logger logger = LoggerFactory.getLogger(SmspParamConfAction.class);

	private ParamConfService paramConfService;

	@Autowired
	private JsmsParamService jsmsParamService;

	@Resource
	public void setParamConfService(ParamConfService paramConfService) {
		this.paramConfService = paramConfService;
	}

	@Action("/smspParamConf/query")
	public String query() {
		Map params = StrutsUtils.getFormData();

		jpage = new JsmsPage();
		if(params.get("currentPage")!=null)
			jpage.setPage(Integer.valueOf(params.get("currentPage").toString()));
		if(params.get("pageRowCount")!=null)
			jpage.setRows(Integer.valueOf(params.get("pageRowCount").toString()));
		else
			jpage.setRows(30);
		Map queryParams = new HashMap();
		queryParams.put("paramType", ParamType.SMSP参数.getValue());
		jpage.setParams(queryParams);
        jpage = jsmsParamService.queryList(jpage);

		return "query";
	}

	@Action("/smspParamConf/edit")
	public String edit() {
		String param_id = StrutsUtils.getParameterTrim("param_id");
		if (NumberUtils.isDigits(param_id)) {
			data = paramConfService.view(Integer.parseInt(param_id));
		} else {
			data = new HashMap<String, Object>();
		}
		return "edit";
	}

	@Action("/smspParamConf/save")
	public void save() {
		data = paramConfService.save(StrutsUtils.getFormData());
		StrutsUtils.renderJson(data);
	}

	@Action("/smspParamConf/updateStatus")
	public void updateStatus() {
		data = paramConfService.updateStatus(StrutsUtils.getFormData());
		StrutsUtils.renderJson(data);
	}


    @Action("/smspParamConf/disable")
    public void disable() {
		Map<String, Object> data = new HashMap<String, Object>();
        Map params = StrutsUtils.getFormData();
		logger.debug("失效SMSP参数操作, params={}", JsonUtil.toJson(params));
        String paramId = (String) params.get("paramId");
        JsmsParam jsmsParam = jsmsParamService.getByParamId(Long.valueOf(paramId));
        logger.debug("查询出jsmsParam={}",JsonUtil.toJson(jsmsParam));
		if(jsmsParam!=null){
			jsmsParam.setParamStatus(ParamStatus.无效.getValue().byteValue());

			jsmsParamService.updateSelective(jsmsParam);
			logger.debug("更新后jsmsParam={}",JsonUtil.toJson(jsmsParam));
			data.put("result", "success");
			data.put("msg", "操作成功");
		}else {
			data.put("result", "fail");
			data.put("msg", "操作失败，参数不存在");
		}
        StrutsUtils.renderJson(data);
    }



	@Action("/smspParamConf/enable")
	public void enable() {
		Map<String, Object> data = new HashMap<String, Object>();
		Map params = StrutsUtils.getFormData();
		logger.debug("有效SMSP参数操作, params={}", JsonUtil.toJson(params));
		String paramId = (String) params.get("paramId");
		JsmsParam jsmsParam = jsmsParamService.getByParamId(Long.valueOf(paramId));
		logger.debug("查询出jsmsParam={}",JsonUtil.toJson(jsmsParam));
		if(jsmsParam!=null){
			jsmsParam.setParamStatus(ParamStatus.有效.getValue().byteValue());

			jsmsParamService.updateSelective(jsmsParam);
			logger.debug("更新后jsmsParam={}",JsonUtil.toJson(jsmsParam));
			data.put("result", "success");
			data.put("msg", "操作成功");
		}else {
			data.put("result", "fail");
			data.put("msg", "操作失败，参数不存在");
		}
		StrutsUtils.renderJson(data);
	}
}
